<?php
namespace App\Http\Controllers;

use App\Http\Logic\AuthLogic;
use App\Http\Requests\Auth\CheckPhoneCodeRequest;
use App\Http\Requests\Auth\ForgetPasswordRequest;
use App\Http\Requests\Auth\LoginRequest;
use App\Http\Requests\Auth\PhoneLoginRequest;
use App\Http\Requests\Auth\RegisterRequest;
use App\Http\Requests\Auth\SellerAuthRequest;
use App\Http\Requests\Auth\SetPasswordRequest;
use App\Http\Requests\Auth\SetPayPasswordRequest;
use App\Http\Requests\Auth\UpdateAvatarRequest;
use App\Http\Requests\Auth\UpdateNameRequest;
use App\Http\Requests\Auth\UpdatePasswordRequest;
use App\Http\Requests\Auth\UpdatePayPasswordRequest;
use App\Http\Requests\Auth\UpdatePhoneRequest;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AuthController extends BaseController
{
    /**
     * 用户注册
     * @param RegisterRequest $request
     * @return JsonResponse
     */
    public function register(RegisterRequest $request): JsonResponse
    {
        $params = $request->all();

        $res = AuthLogic::register($params['email'], $params['password'], $params['country_id'], $params['code']);

        if(false === $res) {
            return $this->error(AuthLogic::getErrorMessage());
        }

        return $this->success($res);
    }

    /**
     * 用户登录
     *
     * @param LoginRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/11 16:24
     */
    public function login(LoginRequest $request): JsonResponse
    {
        $params = $request->all();

        $res = AuthLogic::login($params);

        if(false === $res) {
            return $this->error(AuthLogic::getErrorMessage());
        }

        return $this->success($res);
    }

    /**
     * @notes 手机号登录注册
     * @param PhoneLoginRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/11 16:24
     */
    public function phoneLogin(PhoneLoginRequest $request): JsonResponse
    {
        $phone = $request->get('phone');

        $code = $request->get('code');

        $country_id = $request->get('country_id') ?? '';

        $res = AuthLogic::phoneLogin($phone, $code, $country_id);

        if(false === $res) {
            return $this->error(AuthLogic::getErrorMessage());
        }

        return $this->success($res);
    }

    public function imToken()
    {
        $user_id = auth() -> user() -> id;

        $res = AuthLogic::getImToken($user_id);

        if(false === $res) {
            return $this->error(AuthLogic::getErrorMessage());
        }

        return $this->success($res);
    }

    /**
     * @notes 用户登出
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/11 17:31
     */
    public function logout(): JsonResponse
    {
        auth()->user()->tokens()->delete();

        return $this->success(null, '成功退出登录');
    }

    /**
     * @notes 设置登录密码
     * @param SetPasswordRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/11 16:41
     */
    public function setPassword(SetPasswordRequest $request): JsonResponse
    {
        $password = $request->get('password');

        $code = $request->get('code');

        $user_id = auth() -> user() -> id;

        $res = AuthLogic::setPassword($code, $user_id, $password);

        if(false === $res) {
            return $this->error(AuthLogic::getErrorMessage());
        }

        return $this->success($res);
    }

    /**
     * @notes 修改登录密码
     * @param UpdatePasswordRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/11 16:24
     */
    public function updatePassword(UpdatePasswordRequest $request): JsonResponse
    {
        $params = $request->all();

        $params['user_id'] = auth() -> user() -> id;

        $res = AuthLogic::updatePassword($params);

        if(false === $res) {
            return $this->error(AuthLogic::getErrorMessage());
        }

        return $this->success($res);
    }

    /**
     * @notes 忘记密码
     * @param ForgetPasswordRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/05/09 10:36
     */
    public function forgetPassword(ForgetPasswordRequest $request)
    {
        $params = $request->all();

        $res = AuthLogic::forgetPassword($params);

        if(false === $res) {
            return $this->error(AuthLogic::getErrorMessage());
        }

        return $this->success($res);
    }

    /**
     * @notes 设置支付密码
     * @param SetPayPasswordRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/11 16:45
     */
    public function setPayPassword(SetPayPasswordRequest $request): JsonResponse
    {
        $params = $request->all();

        $params['user_id'] = auth() -> user() -> id;

        $res = AuthLogic::setPayPassword($params);

        if(false === $res) {
            return $this->error(AuthLogic::getErrorMessage());
        }

        return $this->success($res);
    }

    /**
     * @notes 修改支付密码
     * @param UpdatePayPasswordRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/11 17:22
     */
    public function updatePayPassword(UpdatePayPasswordRequest $request): JsonResponse
    {
        $params = $request->all();

        $params['user_id'] = auth() -> user() -> id;

        $res = AuthLogic::updatePayPassword($params);

        if(false === $res) {
            return $this->error(AuthLogic::getErrorMessage());
        }

        return $this->success($res);
    }

    /**
     * @notes 验证手机号验证码是否正确
     * @param CheckPhoneCodeRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/11 17:22
     */
    public function checkPhoneCode(CheckPhoneCodeRequest $request): JsonResponse
    {
        $code = $request->get('code');

        $user_id = auth() -> user() -> id;

        $res = AuthLogic::checkPhoneCode($code, $user_id);

        if(false === $res) {
            return $this->error(AuthLogic::getErrorMessage());
        }

        return $this->success($res);
    }

    /**
     * @notes 修改手机号
     * @param UpdatePhoneRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/11 17:22
     */
    public function updatePhone(UpdatePhoneRequest $request): JsonResponse
    {
        $phone = $request->get('phone');

        $code = $request->get('code');

        $user_id = auth() -> user() -> id;

        $res = AuthLogic::updatePhone($phone, $code, $user_id);

        if(false === $res) {
            return $this->error(AuthLogic::getErrorMessage());
        }

        return $this->success($res);
    }

    /**
     * @notes 修改头像
     * @param UpdateAvatarRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/15 12:32
     */
    public function updateAvatar(UpdateAvatarRequest $request): JsonResponse {

        $path = $request->file('avatar')->store('uploads/avatar', 'public');

        $user_id = auth() -> user() -> id;

        $res = AuthLogic::updateUserInfo($user_id, ['avatar' => $path]);

        if(false === $res) {
            return $this->error(AuthLogic::getErrorMessage());
        }

        return $this->success($res);
    }

    /**
     * @notes 修改昵称
     * @param UpdateNameRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/15 12:32
     */
    public function updateName(UpdateNameRequest $request): JsonResponse {

        $name = $request->get('name');

        $user_id = auth() -> user() -> id;

        $res = AuthLogic::updateUserInfo($user_id, ['name' => $name]);

        if(false === $res) {
            return $this->error(AuthLogic::getErrorMessage());
        }

        return $this->success($res);
    }

    /**
     * @notes 获取当前用户信息
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/11 17:30
     */
    public function info(): JsonResponse
    {
        $user_id = auth() -> user() -> id;

        $data = AuthLogic::getUserInfo($user_id);

        return $this->success($data);
    }

    public function getUserToken(Request $request): JsonResponse
    {
        $user = User::firstWhere('id', $request -> input('id'));

        if(!$user) {
           return $this->error(AuthLogic::getErrorMessage());
        }

        $token = $user->createToken('auth_token')->plainTextToken;

        return $this->success([
            'access_token' => $token,
            'token_type'   => 'Bearer',
            'user'         => $user
        ]);
    }

    public function sellerAuth(SellerAuthRequest $request) : JsonResponse
    {
        $params = $request -> all();

        $user_id = auth() -> user() -> id;

        $data = [
            "type"        => 0,
            "user_id"     => $user_id,
            "card_front"  => $params['card_front'],
            "card_back"   => $params['card_back'],
            "realname"    => $params['realname'],
            "id_card"     => $params['id_card'],
            "start_time"  => $params['start_time'],
            "end_time"    => $params['end_time'],
            "alipay_name" => $params['alipay_name'] ?? null,
            "alipay_no"   => $params['alipay_no'] ?? null,
            "alipay_code" => $params['alipay_code'] ?? null,
            "wechat_name" => $params['wechat_name'] ?? null,
            "wechat_no"   => $params['wechat_no'] ?? null,
            "wechat_code" => $params['wechat_code'] ?? null,
            "contact_type"=> $params['contact_type'],
            "contact_info"=> $params['contact_info']
        ];

        if(key_exists('id', $params) && $params['id']) {
            $res = AuthLogic::editUserIdentity($data, $params['id']);
        } else {
            $res = AuthLogic::addUserIdentity($data);
        }

        if(false === $res) {
            return $this->error(AuthLogic::getErrorMessage());
        }

        return $this->success();
    }

    public function buyerAuth(SellerAuthRequest $request) : JsonResponse
    {
        $params = $request -> all();

        $user_id = auth() -> user() -> id;

        $data = [
            "type"        => 1,
            "user_id"     => $user_id,
            "card_front"  => $params['card_front'],
            "card_back"   => $params['card_back'],
            "realname"    => $params['realname'],
            "id_card"     => $params['id_card'],
            "start_time"  => $params['start_time'],
            "end_time"    => $params['end_time'],
            "contact_type"=> $params['contact_type'],
            "contact_info"=> $params['contact_info']
        ];

        if(key_exists('id', $params)) {
            $res = AuthLogic::editUserIdentity($data, $params['id']);
        } else {
            $res = AuthLogic::addUserIdentity($data);
        }

        if(false === $res) {
            return $this->error(AuthLogic::getErrorMessage());
        }

        return $this->success();
    }
}
